package com.jz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jz.mapper.auto.TStudentMapper;
import com.jz.module.dto.TStudentDto;
import com.jz.module.dto.base.PageInfoDto;
import com.jz.module.entity.TStudent;
import com.jz.module.entity.TStudentExample;
import com.jz.service.StudentService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    @SuppressWarnings("all")
    private TStudentMapper tStudentMapper;

    @Override
    public ResultUtil page(TStudentDto dto) {
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        TStudentExample example = new TStudentExample();
        example.setOrderByClause(" update_time desc,create_time desc");
        List<TStudent> tStudents = tStudentMapper.selectByExample(example);
        PageInfo<TStudent> info = new PageInfo<>(tStudents);
        PageInfoDto<TStudentDto> result = new PageInfoDto<TStudentDto>();
        BeanUtils.copyProperties(info, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil query(TStudentDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        TStudent tStudent = tStudentMapper.selectByPrimaryKey(dto.getId());
        if (tStudent == null) {
            return ResultUtil.error(SysCode.NO_DATA);
        }
        TStudentDto result = new TStudentDto();
        BeanUtils.copyProperties(tStudent, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil save(TStudentDto dto) {
        if (dto == null) {
            return ResultUtil.paramError();
        }
        if (StringUtils.isBlank(dto.getId())) {
            // 新增  用户名查重
            TStudentExample example = new TStudentExample();
            example.createCriteria().andUsernameEqualTo(dto.getUsername());
            List<TStudent> list = tStudentMapper.selectByExample(example);
            if (list == null || list.size() == 0) {
                // insert
                TStudent insert = new TStudent();
                BeanUtils.copyProperties(dto, insert);

                insert.setId(UUIDUtil.getUUID());
                insert.setCreateTime(new Date());
                insert.setUpdateTime(new Date());
                int res = tStudentMapper.insert(insert);
                if (res < 1) {
                    return ResultUtil.error(SysCode.INSERT_ERROR);
                }
            } else {
                return ResultUtil.error("用户名已存在");
            }
        } else {
            TStudent update = new TStudent();
            BeanUtils.copyProperties(dto, update);
            update.setUpdateTime(new Date());
            int res = tStudentMapper.updateByPrimaryKeySelective(update);
            if (res < 1) {
                return ResultUtil.error(SysCode.UPDATE_ERROR);
            }
        }
        return ResultUtil.success();
    }

    @Override
    public ResultUtil delete(TStudentDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        int i = tStudentMapper.deleteByPrimaryKey(dto.getId());
        if (i > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(SysCode.DELETE_ERROR);
    }
}
